package com.sobey.web.function.controller;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.sobey.web.function.model.oracle.Demo;
import com.sobey.web.function.service.demo.DemoService;

@Controller
@RequestMapping("/demo")
public class DemoController {

	private static final Logger LOGGER = Logger.getLogger(DemoController.class);
	
	@Resource(name = "demoService")
	private DemoService demoService;
	
	@RequestMapping("/{id}")
	public String demo01(@PathVariable("id") Long id, Model model) {
		LOGGER.info("demo01  id==" + id);
		model.addAttribute("id", id);
		return "index/index";
	}
	
	/**                                                          
	* 描述 : <描述函数实现的功能>. <br> 
	* 未实现
	*<p> 
		<@AuthenticationPrincipal,负责绑定验证后的用户>  
	 </p>                                                                                                                                                                                                                                                
	* @param user
	* @param id
	* @param model
	* @return                                                                                                      
	*/  
	@RequestMapping("/{id}.do")
	public String demo02(@PathVariable("id") Long id, Model model) {
		LOGGER.info("demo02  id==" + id);
		model.addAttribute("id", id);
		return "demo";
	}
	
	@RequestMapping(value = "/xml/write/{id}.xml", produces = { MediaType.APPLICATION_XML_VALUE
			+ ";charset=UTF-8" })
	@ResponseBody
	@ResponseStatus(HttpStatus.OK)
	public Demo handleWriteXML(@PathVariable("id") Long id, Model model) {
		LOGGER.info("handleWriteXML  id==" + id);
		Demo demo = demoService.getById(id);
		
		return demo;
	}

	@RequestMapping(value = "/xml/write/{id}/{name}.do", produces = { MediaType.APPLICATION_XML_VALUE
			+ ";charset=UTF-8" })
	@ResponseBody
	@ResponseStatus(HttpStatus.OK)
	public Demo handleWriteXMLDo(@PathVariable("id") Long id,
			@PathVariable("name") String name, Model model) {
		LOGGER.info("handleWriteXMLDo  id==" + id);
		Demo demo = demoService.getByName(id, name);
		demoService.save(demo);// 捕获异常
		return demo;
	}

	@RequestMapping(value = "/getdemo/{id}.json",method= RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE
			+ ";charset=UTF-8" })
	@ResponseBody
	@ResponseStatus(HttpStatus.OK)
	public Demo handleWriteJSON(@PathVariable("id") Long id, Model model) {
		LOGGER.info("handleWriteJSON  id==" + id);
		Demo demo = demoService.getById(id);
		return demo;
	}

	@RequestMapping("/jsonmock")
	public void handleJsonMock() {
		// 因为使用了属性拦截器对日期字符串yyyy-MM-dd自动转换为date类型
		String requestBody = "{\"name\":\"王五\",\"age\":30,\"createDate\":\"2014-08-21\",\"modifyDate\":\"2014-08-30\"}";
		try {
//			String result = HttpClientUtil.httpPost(
//					"http://localhost:8080/webmvc/demorest/save", requestBody,
//					MediaType.APPLICATION_JSON_VALUE);
//			LOGGER.info("result======" + result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@RequestMapping("/xmlmock")
	public void handleXmlMock() {
		// 因为使用了属性拦截器对日期字符串yyyy-MM-dd自动转换为date类型
		String requestBody = "<demo><age>40</age><createDate>2014-08-20</createDate><modifyDate>2014-08-28</modifyDate><name>赵四</name></demo>";
		try {
//			String result = HttpClientUtil.httpPost(
//					"http://localhost:8080/webmvc/demorest/savexml",
//					requestBody, MediaType.APPLICATION_XML_VALUE);
//			LOGGER.info("result======" + result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
